/**
 * 用户商品列表接口
 */
const express = require('express')
const shop = express.Router()

// 引入数据库
const db = require('../utils/db')
// 引入加密模块
const crypo = require('../utils/crypo')

/* 获取店铺列表信息 */
shop.get('/shoplist', (req, res) => {
   const sql = `SELECT * FROM shoplists`
   db.query(sql, (err, results) => {
      res.json(results)
   })
})

/* 获取对应店铺的所有商品 */
shop.get('/goods', (req, res) => {
   const { id } = req.query
   const sql = `SELECT * FROM goods WHERE shoplist_id=${id}`
   db.query(sql, (err, results) => {
      if(results.length > 0) res.json({ goods: results, msg: 'ok' })
      else res.json({ goods: [], msg: 'ok' })
   })
})

/* 根据店铺id获取店铺详细信息 */
shop.get('/info', (req, res) => {
   const { id } = req.query
   const sql = `SELECT * FROM shoplists WHERE id=${id}`
   db.query(sql, (err, results) => {
      if (results.length >= 1) {
         res.json({ code: 1, msg: 'ok', shopinfo: results[0] })
      } else res.json({ code: 0, msg: 'not find', shoplist: {} })
   })
})

module.exports = shop